package com.xkdx.guangguang.fragment.blog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.util.AsyncImageLoader;
import com.xkdx.guangguang.wiget.DragImageView;

public class ShowLargeMap extends Activity {
	AsyncImageLoader asyncImageLoader;
	Bitmap bmp;
	private Bitmap b;
	DragImageView im, im2;
	ProgressBar pb;
	Bitmap bmp1;
	private Bitmap b1;

	String Url;
	String thrum;

	RelativeLayout rl;
	int image_height;
	ScrollView scroll;
	boolean isScroll = false;
	boolean isOriLoaded = false;
	DisplayMetrics dm;
	RelativeLayout ll_large;

	private ProgressBar showlarge_pb;
	private boolean isBack = false;

	private int window_width, window_height;// 控件宽度
	private int state_height;// 状态栏的高度

	/**
	 * ViewTreeObserver类是一个视图树的观察者，用来帮助监听某些view的变化
	 */
	private ViewTreeObserver viewTreeObserver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		WindowManager manager = getWindowManager();
		window_width = manager.getDefaultDisplay().getWidth();
		window_height = manager.getDefaultDisplay().getHeight();
		setContentView(R.layout.show_largemap);
		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		asyncImageLoader = new AsyncImageLoader(this);

		im = (DragImageView) findViewById(R.id.largeImage);
		im2 = (DragImageView) findViewById(R.id.largeMiddleImage);
		scroll = (ScrollView) findViewById(R.id.scroll);
		showlarge_pb = (ProgressBar) findViewById(R.id.showlarge_pb);
		ll_large = (RelativeLayout) findViewById(R.id.ll_large);

		Intent intent = getIntent();
		Url = intent.getStringExtra("origeMap");
		thrum = intent.getStringExtra("thumb");

		new Thread() {
			public void run() {
				b1 = asyncImageLoader.getBiggerBitmapFromURL(thrum); // 缩率图
				if (b1 != null) {
					bmp1 = getBiggerBitmap(b1);
					if (bmp1 != null) {
						h.sendEmptyMessage(2);
					}
				}
			};
		}.start();

		new Thread() {
			public void run() {
				b = asyncImageLoader.getBiggerBitmapFromURL(Url); // 原始图
				if (b != null) {
					bmp = getBiggerBitmap(b);
					if (bmp != null) {
						h.sendEmptyMessage(1);
					}
				}
			};

		}.start();

	}

	void getScaleImage(final DragImageView dImage, Bitmap bit) {
		dImage.setmActivity(this);// 注入Activity.
		/** 测量状态栏高度 **/
		viewTreeObserver = dImage.getViewTreeObserver();
		viewTreeObserver
				.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						if (state_height == 0) {
							// 获取状况栏高度
							Rect frame = new Rect();
							getWindow().getDecorView()
									.getWindowVisibleDisplayFrame(frame);
							state_height = frame.top;
							dImage.setScreen_H(window_height - state_height);
							dImage.setScreen_W(window_width);
						}
					}
				});

	}

	Handler h = new Handler() {
		private LinearLayout ll;

		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				if (!isScroll) {
					scroll.setVisibility(View.GONE);
					im2.setVisibility(View.VISIBLE);
					im2.setImageBitmap(bmp);
					if (bmp != null) {
						getScaleImage(im2, bmp);
					}
				} else {
					im2.setVisibility(View.GONE);
					scroll.setVisibility(View.VISIBLE);
					im.setImageBitmap(bmp);
					if (bmp != null) {
						getScaleImage(im, bmp);
					}
				}

				if (showlarge_pb != null) {
					showlarge_pb.setVisibility(View.GONE);
				}

				isOriLoaded = true;

			}

			if (msg.what == 2) { // 缩略图
				if (!isOriLoaded) {
					if (showlarge_pb != null) {
						showlarge_pb.setVisibility(View.VISIBLE);
					}
					if (!isScroll) {
						scroll.setVisibility(View.GONE);
						im2.setVisibility(View.VISIBLE);
						im2.setImageBitmap(bmp1);
						getScaleImage(im2, bmp);
					} else {
						im2.setVisibility(View.GONE);
						scroll.setVisibility(View.VISIBLE);
						im.setImageBitmap(bmp1);
						getScaleImage(im, bmp1);
					}
				}

			}
		};
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("查看大图", "onDestroy");
		if (b != null && !b.isRecycled()) {
			b.recycle(); // 回收图片所占的内存
			b = null; // 提醒系统及时回收
		}

		if (bmp != null && !bmp.isRecycled()) {

			bmp.recycle(); // 回收图片所占的内存
			bmp = null; // 提醒系统及时回收
		}

		if (b1 != null && !b1.isRecycled()) {
			b1.recycle(); // 回收图片所占的内存
			b1 = null; // 提醒系统及时回收
		}
		if (bmp1 != null && !bmp1.isRecycled()) {
			bmp1.recycle(); // 回收图片所占的内存
			bmp1 = null; // 提醒系统及时回收
		}

	}

	/**
	 * 获得实际显示图
	 * 
	 * @param bitmap
	 * @return
	 */
	public Bitmap getBiggerBitmap(Bitmap bitmap) {

		int w = dm.widthPixels;
		int h = dm.heightPixels;
		try {

			int width = bitmap.getWidth();
			int height = bitmap.getHeight();
			Matrix matrix = new Matrix();

			float scale = (float) height / width;
			int newWidth = w;
			int newHeigth = (int) (w * scale);

			if (newHeigth >= h) {
				isScroll = true;
			} else {
				isScroll = false;
			}
			float scaleWidht = 0.0f;
			float scaleHeight = 0.0f;
			// 如果图片高度大于4000
			if (newHeigth > 2000) {
				newHeigth = 2000;
				newWidth = (int) (newHeigth / scale);
				scaleHeight = (float) 2000 / height;
				scaleWidht = (float) scaleHeight;
				if ((float) newWidth - w >= 0) {
					newWidth = w;
				} else
					newWidth = (int) (w * scaleWidht);
			} else {
				scaleWidht = (float) newWidth / width;
				scaleHeight = (float) scaleWidht;

			}

			matrix.postScale(scaleWidht, scaleHeight);
			Bitmap newbmp = null;
			newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
					true);

			// bitmap.recycle();

			return newbmp;

		} catch (Exception e) {
			e.getMessage();
		}
		return null;
	}

	@Override
	protected void onPause() {

		if (isBack) {
			isBack = false;
			overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
		}
		super.onPause();
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			isBack = true;
		}
		return super.onKeyUp(keyCode, event);
	}

}
